package cn.zgwj.user.controller;

import cn.zgwj.basic.exception.BusinessException;
import cn.zgwj.basic.util.AjaxResult;
import cn.zgwj.basic.util.PageList;
import cn.zgwj.user.domain.User;
import cn.zgwj.user.dto.UserDto;
import cn.zgwj.user.query.UserQuery;
import cn.zgwj.user.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user")
@Api(tags = "用户接口",description = "用户接口详细描述")
public class UserController {
    @Autowired
    private IUserService userService;

    //新增或修改   POST
    @PostMapping("/save")
    @ApiOperation(value = "用户添加或修改",notes = "如果有id就是修改否则就是添加")
    public AjaxResult addOrUpdate(@RequestBody User user){
        try {
            if (user.getId() == null) {
                userService.add(user);
            } else {
                userService.update(user);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙,稍后重试");
        }
    }

    //删除    DELETE
    @DeleteMapping("/{id}")  // http://localhost:8080/dept/1
    public AjaxResult removeById(@PathVariable("id") Long id){
        try {
            userService.remove(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me()
                    .setSuccess(false)
                    .setMessage("系统繁忙,稍后重试");
        }
    }

    //获取一条数据   GET
    @GetMapping("/{id}")
    public User findById(@PathVariable("id") Long id){
        return userService.queryById(id);
    }

    //获取所有数据   GET
    @GetMapping("/list")
    public List<User> findAll(){
        return userService.queryAll();
    }

    //分页 + 高级查询   POST
    @PostMapping("/page")
    public PageList<User> queryPage(@RequestBody UserQuery query){
        return userService.queryPage(query);
    }

    /**
     * 批量删除
     *  如果前端传的是 数组,那么在后端可以使用 list 或者 数组 接收
     * @return
     */
    @PatchMapping
    public AjaxResult batchRemove(@RequestBody List<Long>  ids){
        try {
            userService.batchRemove(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me()
                    .setSuccess(false)
                    .setMessage("系统繁忙,稍后重试");
        }

    }

    /**
     * 手机号注册
     * @param userDto
     * @return
     */
    @PostMapping("/phone/register")
    public AjaxResult phoneRegister(@RequestBody UserDto userDto){
        try {
            userService.phoneRegister(userDto);
            return AjaxResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙,稍后重试");
        }

    }



}
